﻿var data;
$(function(){
	
	$.post('user/getUserMenu',null,function(data1){
		data=eval("("+data1+")");
		InitLeftMenu();
		tabClose();
		tabCloseEven();
		initRightWarning();
		setInterval(initRightWarning, 180000);  
	});
});
//初始化左侧
function InitLeftMenu() {
	$("#nav").accordion({animate:true});//为id为nav的div增加手风琴效果，并去除动态滑动效果
		   $.each(data.menus, function(i, n) {//$.each 遍历_menu中的元素
				var menulist ='';
				menulist +='<ul>';
		        $.each(n.menus, function(j, o) {
					menulist += '<li><div><a ref="'+o.menuid+'" href="javascript:void(0)" rel="' + o.url + '" ><span class="icon '+o.icon+'" >&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div></li> ';
		        })
				menulist += '</ul>';
				$('#nav').accordion('add', {
		            title: n.menuname,
		            content: menulist,
		            iconCls: 'icon ' + n.icon
		        });

		    });
		   $('.easyui-accordion li a').click(function(){//当单击菜单某个选项时，在右边出现对用的内容
				var tabTitle = $(this).children('.nav').text();//获取超链里span中的内容作为新打开tab的标题

				var url = $(this).attr("rel");
				var menuid = $(this).attr("ref");//获取超链接属性中ref中的内容
				var icon = getIcon(menuid,icon);

				addTab(tabTitle,url,icon);//增加tab
				$('.easyui-accordion li div').removeClass("selected");
				$(this).parent().addClass("selected");
			}).hover(function(){
				$(this).parent().addClass("hover");
			},function(){
				$(this).parent().removeClass("hover");
			});

			//选中第一个
			var panels = $('#nav').accordion('panels');
			var t = panels[0].panel('options').title;
		    $('#nav').accordion('select', t);
	$('.easyui-accordion li a').click(function(){//当单击菜单某个选项时，在右边出现对用的内容
		var tabTitle = $(this).children('.nav').text();//获取超链里span中的内容作为新打开tab的标题

		var url = $(this).attr("rel");
		var menuid = $(this).attr("ref");//获取超链接属性中ref中的内容
		var icon = getIcon(menuid,icon);

		addTab(tabTitle,url,icon);//增加tab
		$('.easyui-accordion li div').removeClass("selected");
		$(this).parent().addClass("selected");
	}).hover(function(){
		$(this).parent().addClass("hover");
	},function(){
		$(this).parent().removeClass("hover");
	});

	//选中第一个
	var panels = $('#nav').accordion('panels');
	var t = panels[0].panel('options').title;
    $('#nav').accordion('select', t);
}
//获取左侧导航的图标
function getIcon(menuid){
	var icon = 'icon ';
	$.each(data.menus, function(i, n) {
		 $.each(n.menus, function(j, o) {
		 	if(o.menuid==menuid){
				icon += o.icon;
			}
		 })
	})

	return icon;
}

function addTab(subtitle,url,icon){
	if(!$('#tabs').tabs('exists',subtitle)){
		$('#tabs').tabs('add',{
			title:subtitle,
			content:createFrame(url),
			closable:true,
			icon:icon
		});
	}else{
		$('#tabs').tabs('select',subtitle);
		$('#mm-tabupdate').click();
	}
	tabClose();
}

function createFrame(url)
{
	var s = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
	return s;
}

function tabClose()
{
	/*双击关闭TAB选项卡*/
	$(".tabs-inner").dblclick(function(){
		var subtitle = $(this).children(".tabs-closable").text();
		$('#tabs').tabs('close',subtitle);
	})
	/*为选项卡绑定右键*/
	$(".tabs-inner").bind('contextmenu',function(e){
		$('#mm').menu('show', {
			left: e.pageX,
			top: e.pageY
		});

		var subtitle =$(this).children(".tabs-closable").text();

		$('#mm').data("currtab",subtitle);
		$('#tabs').tabs('select',subtitle);
		return false;
	});
}
//绑定右键菜单事件
function tabCloseEven()
{
	//刷新
	$('#mm-tabupdate').click(function(){
		var currTab = $('#tabs').tabs('getSelected');
		var url = $(currTab.panel('options').content).attr('src');
		$('#tabs').tabs('update',{
			tab:currTab,
			options:{
				content:createFrame(url)
			}
		})
	})
	//关闭当前
	$('#mm-tabclose').click(function(){
		var currtab_title = $('#mm').data("currtab");
		$('#tabs').tabs('close',currtab_title);
	})
	//全部关闭
	$('#mm-tabcloseall').click(function(){
		$('.tabs-inner span').each(function(i,n){
			var t = $(n).text();
			$('#tabs').tabs('close',t);
		});
	});
	//关闭除当前之外的TAB
	$('#mm-tabcloseother').click(function(){
		$('#mm-tabcloseright').click();
		$('#mm-tabcloseleft').click();
	});
	//关闭当前右侧的TAB
	$('#mm-tabcloseright').click(function(){
		var nextall = $('.tabs-selected').nextAll();
		if(nextall.length==0){
			//msgShow('系统提示','后边没有啦~~','error');
			alert('后边没有啦~~');
			return false;
		}
		nextall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			$('#tabs').tabs('close',t);
		});
		return false;
	});
	//关闭当前左侧的TAB
	$('#mm-tabcloseleft').click(function(){
		var prevall = $('.tabs-selected').prevAll();
		if(prevall.length==0){
			alert('到头了，前边没有啦~~');
			return false;
		}
		prevall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			$('#tabs').tabs('close',t);
		});
		return false;
	});

	//退出
	$("#mm-exit").click(function(){
		$('#mm').menu('hide');
	})
}

//弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType) {
	$.messager.alert(title, msgString, msgType);
}


//初始化预警提醒框
function initRightWarning() {
	$("#rightWarning").empty();
	if(userPostid=="13"||userPostid=="18"||userPostid=="8"){
		$.post("inductrialInjuryCost/warning",function(data){
			var data=eval("("+data+")");
			if(typeof(data.declaration)!="undefined"&&data.declaration!=""){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='declaration'>&nbsp&nbsp工伤申报预警</a> </p>");
				$("#declaration").click(function(){
					addTab('工伤申报','r/injury/injurymanage?warning=0','');
				});
				
			}
			if(typeof(data.identify)!="undefined"&&data.identify!=""){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='identify'>&nbsp&nbsp工伤鉴定预警</a> </p>");
				$("#identify").click(function(){
					addTab('工伤鉴定','r/injury/injurymanage?warning=1','');
				});
			}
			if(typeof(data.trialTime)!="undefined"&&data.trialTime!=""){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='trialTime'>&nbsp&nbsp开庭时间预警</a> </p>");
				$("#trialTime").click(function(){
					addTab('开庭时间','r/injury/injurymanage?warning=2','');
				});
			};
		});
	};
	
	if(userPostid=="25"||userPostid=="7"){
		$.post("insurance/warning",function(data){
			var data=eval("("+data+")");
			if(typeof(data.fiveInsurances)!="undefined"&&data.fiveInsurances!=""){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='fiveInsurances'>&nbsp&nbsp投不上五险预警</a> </p>");
				$("#fiveInsurances").click(function(){
					addTab('投不上五险','r/staff/insuranceContract?warning=3','');
				});
			}
			if(typeof(data.contract_30)!="undefined"&&data.contract_30 !=""){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='contract_30'>&nbsp&nbsp合同快过期预警</a> </p>");
				$("#contract_30").click(function(){
					addTab('合同快过期','r/staff/insuranceContract?warning=1','');
				});
			}
			if(typeof(data.contractEnd)!="undefined"&&data.contractEnd !=""){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='contractEnd'>&nbsp&nbsp合同已过期预警</a> </p>");
				$("#contractEnd").click(function(){
					addTab('合同已过期','r/staff/insuranceContract?warning=2','');
				});
			}
			if(typeof(data.noContract)!="undefined"&&data.noContract !=""){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='noContract'>&nbsp&nbsp未签合同预警</a> </p>");
				$("#noContract").click(function(){
					addTab('未签合同','r/staff/insuranceContract?warning=0','');
				});
			}
		});
	};
	
	if(userPostid=="12"||userPostid=="17"||userPostid=="5"){
		$.post("staffInfo/r",{contractLimit:"0",page:-1,rows:-1},function(data){
			var data=eval("("+data+")");
			if(parseInt(data.total)!=null&&!(isNaN(parseInt(data.total)))&&parseInt(data.total)>0){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='contract_0'>&nbsp&nbsp未签合同预警</a> </p>");
				$("#contract_0").click(function(){
					addTab('未签合同','r/staff/staffContractList?warning=contract_0','');
				});
			}
		});
		
		$.post("staffInfo/r",{contractLimit:"1",page:-1,rows:-1},function(data){
			var data=eval("("+data+")");
			if(parseInt(data.total)!=null&&!(isNaN(parseInt(data.total)))&&parseInt(data.total)>0){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='contract_1'>&nbsp&nbsp合同或协议快过期</a> </p>");
				$("#contract_1").click(function(){
					addTab('合同或协议快过期','r/staff/staffContractList?warning=contract_1','');
				});
			}
		});
		
		$.post("staffInfo/r",{contractLimit:"2",page:-1,rows:-1},function(data){
			var data=eval("("+data+")");
			if(parseInt(data.total)!=null&&!(isNaN(parseInt(data.total)))&&parseInt(data.total)>0){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='contract_2'>&nbsp&nbsp合同或协议已过期</a> </p>");
				$("#contract_2").click(function(){
					addTab('合同或协议已过期','r/staff/staffContractList?warning=contract_2','');
				});
			}
		});
		
		$.post("staffInfo/r",{retireRemind:"0",page:-1,rows:-1},function(data){
			var data=eval("("+data+")");
			if(parseInt(data.total)!=null&&!(isNaN(parseInt(data.total)))&&parseInt(data.total)>0){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='retire_0'>&nbsp&nbsp即将退休预警</a> </p>");
				$("#retire_0").click(function(){
					addTab('即将退休','r/staff/staffContractList?warning=retire_0','');
				});
			}
		});
		
		$.post("staffInfo/r",{retireRemind:"1",page:-1,rows:-1},function(data){
			var data=eval("("+data+")");
			if(parseInt(data.total)!=null&&!(isNaN(parseInt(data.total)))&&parseInt(data.total)>0){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='retire_1'>&nbsp&nbsp已达退休年龄预警</a> </p>");
				$("#retire_1").click(function(){
					addTab('已达退休年龄','r/staff/staffContractList?warning=retire_1','');
				});
			}
		});
		
		$.post("recruit/r",{s_warningRemind:"1",page:-1,rows:-1},function(data){
			var data=eval("("+data+")");
			if(parseInt(data.total)!=null&&!(isNaN(parseInt(data.total)))&&parseInt(data.total)>0){
				$("#rightWarning").append("<p><a href='javascript:void(0)'  id='recruitRemind_1'>&nbsp&nbsp招聘任务预警</a> </p>");
				$("#recruitRemind_1").click(function(){
					addTab('招聘任务','r/recruit/recruitList?warning=recruitRemind_1','');
				});
			}
		});
	};
	
}